﻿using DevComponents.DotNetBar;
using Kimd.Common;
using Kimd.Machine;
using Kimd.Motion;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkStation.Common;

namespace WorkStation.UI
{
    public partial class FormMoveTray : Office2007Form
    {
        private Tray _tray;
        private string _moduleName;
        public FormMoveTray(Tray tray, string moduleName)
        {
            InitializeComponent();
            this._tray = tray;
            this._moduleName = moduleName;  
            this.EnableGlass = false;
        }

        private void FormSingleAxisMoveStep_Load(object sender, EventArgs e)
        {
            cB_Index.Items.Clear();
            for (int i = 0; i < _tray.RowCount * _tray.ColCount; i++)
            {
                cB_Index.Items.Add(i.ToString());
            }
            cB_Nozzle.Items.Clear();
            for (int i = 0; i < _tray.NozzleCount; i++)
            {
                cB_Nozzle.Items.Add(i + 1);
            }
            cB_Nozzle.SelectedIndex = 0;
            cB_Index.SelectedIndex = 0;
        }
        int currentIndex = 0;
        int nozzleIndex = 0;
        private async void btn_Confirm_Click(object sender, EventArgs e)
        {
            this.btn_Confirm.Enabled = false;
            if(cB_Index.SelectedIndex != -1)
            {
                if (!SingletonProvider<MachineResource>.Instance.ModulePaths.ContainsKey(_moduleName))
                    return;
                if (SingletonProvider<MachineResource>.Instance.ModulePaths[_moduleName].ContainsKey(_tray.Name))
                {
                    PointPath pp = SingletonProvider<MachineResource>.Instance.ModulePaths[_moduleName][_tray.Name];
                    string errInfo = "";
                    currentIndex = Convert.ToInt32(cB_Index.SelectedItem);
                    nozzleIndex = Convert.ToInt32(cB_Nozzle.SelectedItem) - 1;
                    await Task.Run(() =>
                    {
                        pp.Prepare(MachineResource.Instance.MachineAxis, MachineResource.Instance.MachineCylinder, _tray, currentIndex, nozzleIndex);
                        pp.GoTo(MachineResource.Instance.MachineAxis, MachineResource.Instance.MachineCylinder, MachineResource.Instance.MachineSensor, out errInfo, currentIndex);
                        currentIndex++;
                        if (currentIndex == _tray.RowCount * _tray.ColCount)
                            currentIndex = 0;
                        if (!string.IsNullOrEmpty(errInfo))
                        {
                            this.BeginInvoke(new Action(() =>
                            {
                                SystemInfo.ShowToastInfo(this, errInfo, true);
                                this.btn_Confirm.Enabled = true;
                                //e.GridCell.Value = "运动至此";
                            }));
                        }
                        else
                        {
                            this.BeginInvoke(new Action(() =>
                            {
                                SystemInfo.ShowToastInfo(this, "移动完成", false);
                                cB_Index.SelectedIndex = cB_Index.Items.IndexOf(currentIndex.ToString());
                                this.btn_Confirm.Enabled = true;
                            }));
                        }
                    });
                }
            }
            else
            {
                if (SingletonProvider<MachineResource>.Instance.ModulePaths[_moduleName].ContainsKey(_tray.Name))
                {
                    PointPath pp = SingletonProvider<MachineResource>.Instance.ModulePaths[_moduleName][_tray.Name];
                    string errInfo = "";
                    currentIndex = Convert.ToInt32(cB_Index.SelectedItem);
                    nozzleIndex = Convert.ToInt32(cB_Nozzle.SelectedItem) - 1;
                    await Task.Run(() =>
                    {
                        pp.Prepare(MachineResource.Instance.MachineAxis, MachineResource.Instance.MachineCylinder, _tray, currentIndex, nozzleIndex);
                        Tray tray = SingletonProvider<MachineResource>.Instance.ModuleTrays[_moduleName][_tray.Name];
                        if (currentIndex == tray.RowCount * tray.ColCount - 1)
                            currentIndex = 0;
                        pp.GoTo(MachineResource.Instance.MachineAxis, MachineResource.Instance.MachineCylinder, MachineResource.Instance.MachineSensor, out errInfo, currentIndex);
                        currentIndex++;
                        if (!string.IsNullOrEmpty(errInfo))
                        {
                            this.BeginInvoke(new Action(() =>
                            {
                                SystemInfo.ShowToastInfo(this, errInfo, true);
                                this.btn_Confirm.Enabled = true;
                                //e.GridCell.Value = "运动至此";
                            }));
                        }
                        else
                        {
                            this.BeginInvoke(new Action(() =>
                            {
                                SystemInfo.ShowToastInfo(this, "移动完成", false);
                                cB_Index.SelectedIndex = -1;
                                this.btn_Confirm.Enabled = true;
                            }));
                        }
                    });
                }
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            if (SingletonProvider<MachineResource>.Instance.ModulePaths.ContainsKey(_moduleName))
            {
                if (SingletonProvider<MachineResource>.Instance.ModulePaths[_moduleName].ContainsKey(_tray.Name))
                {
                    PointPath pp = SingletonProvider<MachineResource>.Instance.ModulePaths[_moduleName][_tray.Name];
                    pp.Stop(SingletonProvider<MachineResource>.Instance.MachineAxis);
                }
            }
        }

        private void cB_Index_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cB_Index.SelectedIndex == -1)
                btn_Confirm.Text = "下一个";
            else
                btn_Confirm.Text = "运动至";
        }
    }
}
